//
//  XTCaseViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTCaseViewController.h"
#import "XTCaseDetailsViewController.h"
#import "XTCaseYeMianTableViewCell.h"
#import "XTNetworkHandle.h"
#import "XTCase.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "XTMyLoginViewController.h"
#import "AppDelegate.h"

@interface XTCaseViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *caseArr;
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)MXPullDownMenu *menu;
@property(nonatomic, retain)NSString *chooserStyle;
@property(nonatomic, retain)NSString *chooserType;
@property(nonatomic, retain)NSString *chooserArea;
@property(nonatomic, retain)NSString *chooserCost;
@end

@implementation XTCaseViewController


//下拉出选项菜单
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"装修案例";
    self.navigationController.navigationBar.translucent = NO;
    //我的
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"我的" style:UIBarButtonItemStylePlain target:self action:@selector(my)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
    self.chooserStyle = @"0";
    self.chooserType = @"0";
    self.chooserArea = @"0";
    self.chooserCost = @"0";
  
    self.caseArr = [NSMutableArray array];
    //创建TableView
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -113) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    _table.tag = 1000;
    _table.rowHeight = 200;
    [self.view addSubview:_table];
    [_table release];
    self.isUpLoading = NO;
    // 上拉下拉功能
    [self addHeader];
    [self addFooter];
    //[self creatNotificationCenter];
    
}
- (void)my
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}
- (void)rightButtonAction
{
    //下拉出选项
    self.myCount = self.myCount + 1;
    if (self.myCount == 1) {
        NSArray *testArray;
        testArray = @[ @[ @"风格", @"现代", @"简约", @"韩式" , @"地中海" , @"欧式",@"中式",@"日式",@"新古典",@"宜家",@"田园",@"混搭",@"清新",@"小资" ], @[@"户型",@"一居",@"两居",@"三居",@"四居",@"小户型",@"大户型",@"别墅",@"复式",@"公寓",@"跃层",@"LOFT",@"阁楼"], @[@"面积", @"50平米以下", @"50-60平米", @"60-90平米",@"90-120平米",@"120平米以上"],@[@"预算",@"5万以下",@"5-10万",@"10-20万",@"20-40万",@"40万以上"] ];
        MXPullDownMenu *menu = [[MXPullDownMenu alloc] initWithArray:testArray selectedColor:[UIColor blackColor]];
        menu.delegate = self;
        menu.tag = 10001;
        menu.frame = CGRectMake(0, 0, self.view.frame.size.width , menu.frame.size.height);
        [self.view addSubview:menu];
    } else if (self.myCount % 2 == 0) {
        MXPullDownMenu *menu = (MXPullDownMenu *)[self.view viewWithTag:10001];
        menu.hidden = YES;
        NSLog(@"%@, %@, %@, %@", self.chooserStyle, self.chooserType, self.chooserArea, self.chooserCost);
        [self addHeader];
        
    }
   else if (!(self.myCount % 2 == 0 && self.myCount == 1)){
        MXPullDownMenu *menu = (MXPullDownMenu *)[self.view viewWithTag:10001];
        
        menu.hidden = NO;
    }
}
// 实现代理
#pragma mark - MXPullDownMenuDelegate

- (void)PullDownMenu:(MXPullDownMenu *)pullDownMenu didSelectRowAtColumn:(NSInteger)column row:(NSInteger)row
{
    NSLog(@"%ld -- %ld", column, row);
    if (column == 0) {
        if (row == 0) {
            self.chooserStyle = @"0";
        } else if (row < 10) {
            NSString *str = [NSString stringWithFormat:@"%ld0%ld", column + 1, row];
            self.chooserStyle = str;
        } else {
            self.chooserStyle = [NSString stringWithFormat:@"%ld%ld", column + 1, row];
        }
    }
    if (column == 1) {
        if (row == 0) {
            self.chooserType = @"0";
        } else if (row < 10) {
            NSString *str = [NSString stringWithFormat:@"%ld0%ld", column + 1, row];
            self.chooserType = str;
        } else {
            self.chooserType = [NSString stringWithFormat:@"%ld%ld", column + 1, row];
        }
    }
    if (column == 2) {
        if (row == 0) {
            self.chooserArea = @"0";
        } else if (row < 10) {
            NSString *str = [NSString stringWithFormat:@"%ld0%ld", column + 1, row];
            self.chooserArea = str;
        } else {
            self.chooserArea = [NSString stringWithFormat:@"%ld%ld", column + 1, row];
        }
    }
    if (column == 3) {
        if (row == 0) {
            self.chooserCost = @"0";
        } else if (row < 10) {
            NSString *str = [NSString stringWithFormat:@"%ld0%ld", column + 1, row];
            self.chooserCost = str;
        } else {
            self.chooserCost = [NSString stringWithFormat:@"%ld%ld", column + 1, row];
        }
    }
    
    
}

//解析数据
- (void)getData:(NSInteger)nextCursor
{
#warning 封装AFN的调用
    // 参数1: 网络接口
    // 参数2: 返回数据的Block 参数result中是网络请求的返回数据
    
    NSString *str = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/project?start=%ld&count=10&user=(null)&style=%@&type=%@&area=%@&cost=%@",nextCursor,self.chooserStyle, self.chooserType, self.chooserArea, self.chooserCost];
    NSLog(@"%@", str);
    [XTNetworkHandle getDataWithURL:str completion:^(id result) {
        // result为请求接口的返回数据
        //NSLog(@"~~~~~~%@", result);
        self.nextCursor = [[result objectForKey:@"start"]integerValue] + 10;
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.caseArr removeAllObjects];
        }
        
        
        NSMutableArray *arr = [result objectForKey:@"projects"];
        for (NSDictionary *tempDic in arr) {
            XTCase *xtCase = [[XTCase alloc]init];
            [xtCase setValuesForKeysWithDictionary:tempDic];
            xtCase.mYdescription = [tempDic objectForKey:@"description"];
            
            
            [_caseArr addObject:xtCase];
            
            [xtCase release];
            //NSLog(@"!!!!!%@",_caseArr);
            
        }
        //结束底部刷新效果
        [self.table footerEndRefreshing];
        //结束头部刷新效果
        [self.table headerEndRefreshing];
        //重新加载数据
        [self.table reloadData];
    }];
    
}
//下拉刷新
- (void)addHeader
{
    __block XTCaseViewController *cVC = self;
    // 添加下拉刷新头部控件
    [self.table addHeaderWithCallback:^{
        cVC.isUpLoading = NO;
        //重新请求数据
        [cVC getData:10];
    }];
    //自动刷新
    [self.table headerBeginRefreshing];
}
- (void)addFooter
{
    __block XTCaseViewController *xVC = self;
     // 添加上拉刷新尾部控件
    [self.table addFooterWithCallback:^{
        xVC.isUpLoading = YES;
        //请求更多数据
        [xVC getData:xVC.nextCursor];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _caseArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCaseYeMianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[[XTCaseYeMianTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"]autorelease];
    }
    
    XTCase *xtCase = _caseArr[indexPath.row];
    cell.lable1.text = xtCase.title;
    cell.lable2.text = xtCase.date;
    
    //加载网络图片
    //大图片
    id photoId = [[xtCase.photos objectAtIndex:0]objectForKey:@"id"];
    NSString *string = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w640_h0_m0.jpg", photoId];
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:string]placeholderImage:[UIImage imageNamed:@"3-1"]];
    
    //头像
    NSString *str = [[xtCase.user objectForKey:@"userImage"]objectForKey:@"small"];
    [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:str]placeholderImage:[UIImage imageNamed:@"my.jpg"]];
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XTCaseDetailsViewController *xVC = [[[XTCaseDetailsViewController alloc]init]autorelease];
    XTCase *xtCase = _caseArr[indexPath.row];
    xVC.xtCaseXiang = xtCase;
    xVC.desc = xtCase.mYdescription;
    xVC.user1 = xtCase.user;
    [self.navigationController pushViewController:xVC animated:YES];
}
- (void)creatNotificationCenter
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changeColor:) name:@"color" object:nil];
}
- (void)changeColor:(NSNotification *)noti
{
    self.navigationController.navigationBar.translucent = NO;
    self.backgroundColor = noti.object;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:self.backgroundColor] forBarMetrics:UIBarMetricsDefault];
}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:animated];
//    //    NSLog(@"viewWillDisappear");
//    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
//}
//
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    //    NSLog(@"viewWillAppear");
//    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
